package edu.lagou.service.impl;

import edu.lagou.dao.TokenDao;
import edu.lagou.pojo.Authcode;
import edu.lagou.pojo.Token;
import edu.lagou.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

@Service
public class TokenServiceImpl implements TokenService {

    @Autowired
    private TokenDao tokenDao;

    @Override
    public String getEmail(String token) {
        Token tToken = findByToken(token);
        return tToken.getEmail();
    }

    @Override
    public void save(String email, String token) {
        Token tToken = new Token();
        tToken.setEmail(email);
        tToken.setToken(token);
        tokenDao.save(tToken);
    }

    protected Token findByToken(String token) {
        Token tToken = new Token();
        tToken.setToken(token);
        Example<Token> example = Example.of(tToken);
        return tokenDao.findOne(example).get();
    }
}
